<template>
  <div>
    <BaseSearch :config="searchConfig" v-bind="form" ref="search"></BaseSearch>
    <BaseTable :columns="columns" v-bind="table"></BaseTable>
  </div>
</template>

<script>
import BaseSearch from '@/components/BaseSearch/index.vue'
import BaseTable from '@/components/BaseTable/index.vue'
import { FormTableMixin } from '@/mixins/form-table'
import { CREDITEXTENSION_RECORD_STATUS } from '@/constants/auditModel'
import http from '@/utils/request'
const service = async params => {
  const { records, total } = await http.get('/credit_applys', {
    params: { ...params, applyType: 0, listType: 2 },
  })
  if (records && records.length) {
    records.forEach(item => {
      item.newStatus = item.auditStatus + item.status
      item.statusText = CREDITEXTENSION_RECORD_STATUS[item.newStatus]
    })
  }
  return {
    data: records,
    total: total,
  }
}

export default {
  name: 'creditExtensionRecord',
  components: { BaseSearch, BaseTable },
  mixins: [FormTableMixin(service)],
  data() {
    const isCanDetail = this.$checkAccess('MENU_CREDIT_EXTENSION_RECORD_DETAIL')
    return {
      searchConfig: [
        { component: 'input', prop: 'searchStr', label: '搜索', placeholder: '企业名称/流水号' },
        {
          component: 'select',
          prop: 'fundCode',
          label: '资金方',
          options: [],
        },
        {
          component: 'select',
          prop: 'appCode',
          label: '所属项目',
          options: [],
        },
      ],
      columns: [
        { label: '流水号', prop: 'applyNo' },
        {
          label: '所属项目',
          prop: 'appName',
        },
        { label: '企业名称', prop: 'tenantName' },
        { label: '产品名称', prop: 'productName' },
        { label: '资金方', prop: 'fundName' },
        { label: '授信金额', prop: 'quota' },
        { label: '受理时间', prop: 'createDate' },
        { label: '审核状态', prop: 'statusText' },
        {
          label: '操作',
          render: (val, row) => {
            return (
              <div>
                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.goDetail(row)
                    }}>
                    详情
                  </el-button>
                )}
              </div>
            )
          },
        },
      ],
    }
  },
  methods: {
    goDetail(item) {
      const data = JSON.stringify(item)
      this.$router.push({
        name: 'creditExtensionRecordDetail',
        query: {
          applyId: item.id,
          appCode: item.appCode,
          uscCode: item.uscCode || '',
          itemData: data,
        },
      })
    },
    // 资方列表
    async getFundNameRequest() {
      const res = await this.$http.get('/funds/list')
      if (res && res.length) {
        this.searchConfig[1].options = res.map(item => {
          return {
            label: item.name,
            value: item.code,
          }
        })
      }
    },
    //获取来源
    async getSourceListRequest() {
      const sourceList = await this.$http.get(`/app/list`)
      if (sourceList && sourceList.length) {
        const list = sourceList.map(item => {
          return {
            label: item.name,
            value: item.code,
          }
        })
        this.searchConfig[2].options = [...list]
      }
    },
  },
  mounted() {
    this.getSourceListRequest()
    this.getFundNameRequest()
  },
}
</script>

<style lang="scss" scoped></style>
